﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;

namespace DemoGame1
{
    public class MyButton : VisibleGameEntity
    {
        private String _ButtonName = String.Empty;

        public String ButtonName
        {
            get { return _ButtonName; }
            set { _ButtonName = value; }
        }

        private void InitMyButton(ContentManager content, string[] strTexture, int nTextures, Vector2 _topleft, Vector2 _size)
        {
            TopLeft = _topleft;
            Size = _size;
            Texture2D[] textures = new Texture2D[nTextures];
            Sprites = new List<My2DSprite>();
            for (int i = 0; i < nTextures; i++)
                textures[i] = content.Load<Texture2D>(strTexture[i]);
            My2DSprite tempsprite = new My2DSprite(textures, TopLeft);
            Sprites.Add(tempsprite);
            nSprites = 1;
        }
        public MyButton()
        {
        }
        public MyButton(ContentManager content, string strTexturePrefix, int nTextures, Vector2 _topleft, Vector2 _size)
        {
            string[] strTexture = new string[nTextures];
            for (int i = 0; i < nTextures; i++)
                strTexture[i] = strTexturePrefix + (i + 1).ToString("00");
            InitMyButton(content, strTexture, nTextures, _topleft, _size);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public void SetNextFrame(int nFrame)
        {
            for (int i = 0; i < nSprites; i++)
            {
                Sprites[i].SetNextFrame(nFrame);
            }
        }
        public void NextFrame()
        {
            for (int i = 0; i < nSprites; i++)
            {
                Sprites[i].NextFrame();
            }
        }

        public bool isEndFrame()
        {
            if (Sprites[nSprites - 1].iTextures == Sprites[nSprites - 1].nTextures - 1)
                return true;
            return false;
        }

        public override void Draw(SpriteBatch sb, GameTime gameTime)
        {
            if (Visible == true)
            {
                for (int i = 0; i < nSprites; i++)
                {
                    sprites[i].Draw(sb, gameTime, _TopLeft, _Size);
                }
            }
        }
        public override void Draw(SpriteBatch sb, GameTime gameTime, Vector2 _TopLeft, Vector2 _Size)
        {
            for (int i = 0; i < nSprites; i++)
            {
                sprites[i].Draw(sb, gameTime, _TopLeft, _Size);
            }
        }
        public  void DrawwithDepth(SpriteBatch sb, GameTime gameTime, Vector2 _TopLeft, Vector2 _Size,float depth)
        {
            for (int i = 0; i < nSprites; i++)
            {
                sprites[i].DrawwithDepth(sb, gameTime, _TopLeft, _Size,depth);
            }
        }
        public MyButton(ContentManager content, XmlElement element)
        {
            //MyButton backGroundButton;
            string strTexturePrefix = String.Empty;
            int nTexture = 0;
            Vector2 topleft = new Vector2();
            Vector2 size = new Vector2();
            foreach (XmlElement elm in element.ChildNodes)
            {
                if (elm.Name == "Texture")
                {
                    strTexturePrefix = elm.InnerText.Trim();
                }

                if (elm.Name == "nTexture")
                {
                    nTexture = Convert.ToInt32(elm.InnerText.Trim());
                }

                if (elm.Name == "TopLeft")
                {
                    foreach (XmlElement tl in elm.ChildNodes)
                    {
                        if (tl.Name == "x")
                        {
                            topleft.X = (float)Convert.ToInt32(tl.InnerText.Trim());
                        }
                        if (tl.Name == "y")
                        {
                            topleft.Y = (float)Convert.ToInt32(tl.InnerText.Trim());
                        }
                    }
                }
                if (elm.Name == "Size")
                {
                    foreach (XmlElement sz in elm.ChildNodes)
                    {
                        if (sz.Name == "x")
                        {
                            size.X = (float)Convert.ToInt32(sz.InnerText.Trim());
                        }
                        if (sz.Name == "y")
                        {
                            size.Y = (float)Convert.ToInt32(sz.InnerText.Trim());
                        }
                    }
                }
            }
            string[] strTexture = new string[nTexture];
            for (int i = 0; i < nTexture; i++)
                strTexture[i] = strTexturePrefix + (i + 1).ToString("00");
            InitMyButton(content, strTexture, nTexture, topleft, size);
        }
        public MyButton(ContentManager content, string strXML)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(strXML);
            XmlElement elm = doc.DocumentElement;
            Vector2 pos = new Vector2();
            Vector2 size = new Vector2();
            string[] buttontextures = new string[100];
            int nTextures = 1;
            foreach (XmlElement att in elm.ChildNodes)
            {

                if (att.Name == "x")
                    pos.X = (float)Convert.ToDouble(att.InnerText);
                if (att.Name == "y")
                    pos.Y = (float)Convert.ToDouble(att.InnerText);
                if (att.Name == "width")
                    size.X = (float)Convert.ToDouble(att.InnerText);
                if (att.Name == "height")
                    size.Y = (float)Convert.ToDouble(att.InnerText);
                if (att.Name == "textures")
                {
                    nTextures = att.ChildNodes.Count;
                    int ibuttontexture = 0;
                    foreach (XmlElement texture in att.ChildNodes)
                    {
                        buttontextures[ibuttontexture] = texture.InnerText;
                        ibuttontexture++;

                    }

                }
            }
            InitMyButton(content, buttontextures, nTextures, pos, size);
        }


    }


}
